package com.qdy.api;

import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;

import com.qdy.entity.dynamic.Dynamic;
import com.qdy.entity.dynamic.DynamicComment;
import com.qdy.entity.dynamic.DynamicLike;
import com.qdy.entity.exercise.Exercise;
import com.qdy.service.DynamicService;
import com.qdy.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户动态接口
 */
@RestController
@RequestMapping("/api/v1/dynamic")
public class DynamicAPIController {

    @Autowired
    private DynamicService dynamicService;

    @RequestMapping(value = "/findInfo/{dynamicId}" ,method = RequestMethod.GET,
                    produces = {"application/json;charset=UTF-8"})
    public Object findInfo(@PathVariable("dynamicId") Long dynamicId)throws ServiceException{
        try{
            return RestResultGenerator.genSuccessResult(dynamicService.getDynamicInfo(dynamicId));
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 发布动态
     * @param dynamic
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/release" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object release( Dynamic dynamic)throws ServiceException {
         try{
             dynamic.setUser(HttpUtil.getUser());
            dynamicService.saveDynamic(dynamic);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("发布失败",e.getMessage());
        }
    }

    /**
     * 加载数据
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/load" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object loadByAll()throws ServiceException {
        try{

            return RestResultGenerator.genSuccessResult(  dynamicService.findByAll());
        }catch (Exception e){
            throw new ServiceException("加载失败",e.getMessage());
        }
    }

    /**
     * 点赞
      * @param dynamicId
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/like/{dynamicId}" ,method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public Object like(@PathVariable("dynamicId") Long dynamicId)throws ServiceException {
        try{
            DynamicLike like=new DynamicLike();
            like.setDynamicId(dynamicId);
            like.setUser(HttpUtil.getUser());
            dynamicService.saveLike(like);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("加载失败",e.getMessage());
        }
    }

    /**
     * 发表评论
     *
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/sendComment/" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object sendComment(DynamicComment comment)throws ServiceException {
        try {
            comment.setUser(HttpUtil.getUser());
            dynamicService.saveComment(comment);
            return  RestResultGenerator.genSuccessResult();
        }catch (Exception e){
              throw new ServiceException("发表评论失败",e.getMessage());
        }
    }
}
